package com.shopping.servlet.user;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.shopping.dao.UserDao;
import com.shopping.dao.impl.UserDaoImpl;
import com.shopping.pojo.User;
import com.shopping.service.UserService;

@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public AddServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取所有参数
		Map<String, String[]> map = request.getParameterMap();
		for(String key : map.keySet()) {
			System.out.println(key + ": "+ map.get(key)[0] + " " + map.get(key)[0].getClass().toString());
		}
		String mobile = request.getParameter("mobile");
		if(mobile.equals(""))
			mobile = null;
		User user = new User(map.get("username")[0],map.get("password")[0],map.get("email")[0],Integer.valueOf(map.get("sex")[0]),mobile,new Date(),1);
		System.out.println(user);
		
		PrintWriter out =  response.getWriter();
		response.setContentType("application/json;charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		UserDao ud = new UserDaoImpl();
		boolean b = ud.insert(user);
		
		JSONObject jo = new JSONObject();
		int code;
		if(b==true) {
			jo.put("code", 0);
			jo.put("msg","");
		}
		else {
			jo.put("code", 1);
			jo.put("msg", "");
		}
		jo.put("count", 1);
		
		out.println(jo);
		
		
	}

}
